﻿<%@ Page Language="C#" EnableViewState="false" EnableEventValidation="false" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Portal.Core" %>
<%@ Import Namespace="Portal.Core.Helper" %>
<%@ Import Namespace="Portal.Core.Security" %>
<%@ Import Namespace="Portal.Data" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<%@ Import Namespace="Portal.Data.Webparts" %>
<html>
<head runat="server">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--meta http-equiv="refresh" content="2700" /-->
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    <meta name='robots' content='INDEX,FOLLOW' />
    <link rel='SHORTCUT ICON' href='/favicon.ico' />
    <script type="text/javascript">
        var _gaq = _gaq || [];
		_gaq.push(['_setAccount', 'UA-48451683-1']);
		_gaq.push(['_trackPageview']);
		
		_gaq.push(['b._setAccount', 'UA-56164333-1']);
		_gaq.push(['b._trackPageview']);
		
		_gaq.push(['c._setAccount', 'UA-55164618-1']);
		_gaq.push(['c._trackPageview']);
		(function() {
    		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    		//ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; 
    		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
		
        //google plus still init
        (function () {
            var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
            po.src = 'https://apis.google.com/js/platform.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
        })();
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <link href='/Themes/new/css/style.css' rel='stylesheet' type='text/css' />
        <script type='text/javascript'>var basepath = '<%=CoreContext.absStaticFile%>'; var staticpath = '<%=CoreContext.absStaticFile%>'; var isMobile = ('<%=Portal.Core.Utils.MyCheck.IsMobileClient()%>' == 'True');</script>
        <script src="/Themes/js/min.js" type="text/javascript"></script>
        <script src='/Themes/JS/main.js' type='text/javascript' > </script>        
        <asp:PlaceHolder ID="phContent" runat="server"></asp:PlaceHolder> 
        
        <%if (Portal.Core.Utils.MyCheck.IsMobileClient()){%>
			<script type="text/javascript" src="http://truyengicungco.gsspcln.jp/sdk/t/21846.js"></script>
        <% } %>
        <div id="hidden_content" style="display:none"></div>		                   
    </form>
</body>
</html>
<script runat="server">
    private Category _cat;
    private bool _isDetail;
    private readonly StringBuilder _metaDataHeader = new StringBuilder();
    private WebpartPage _wpage;
    private string keyUniq = string.Empty;
    protected override void OnInit(EventArgs e)
    {
        if (CoreContext.UseHtmlCache)
        {
            keyUniq = Portal.Core.Utils.Process.GetUniqueUrl(Request.RawUrl);
            var data = CacheHelper.Instance.Get(keyUniq);
            if (data != null && ((string)data).Length > 0)
            {
                Response.Write(data);
                Response.End();
            }
        }
        try
        {
            //Add some tag in Header control
            var virtualPage = Request["VirtualPage"];
            if (!string.IsNullOrEmpty(virtualPage))
            {
                _wpage = WebpartPage.FindByName(virtualPage);
                SetupPage();
            }
            else
            {
                SelectPage();
            }
            Title = _cat.FullName + PortalContext.TitleExtension(Request);

            if (!_isDetail)
            {
                //listpage, add for SEO
                _metaDataHeader.AppendFormat("<meta name='description' content='{0}'/>", _cat.Description);
                //_metaDataHeader.AppendFormat("<meta name='keywords' content='{0}'/>", _cat.SeoKeywords());
                var ltrH = new LiteralControl(_metaDataHeader.ToString());
                Header.Controls.Add(ltrH);

                //add H1,H2 default
                phContent.Controls.Add(new LiteralControl(string.Format("<h1 class='invisible'>{0}</h1><h2 class='invisible'>{0}</h2>", _cat.FullName)));
            }
        }
        catch (Exception ex)
        {
            throw new HttpNotFoundException();
        }
    }
    private void SelectPage()
    {
        string pagename = Request["portalpage"] ?? "default";

        switch (pagename.ToLower())
        {
            case "default":
                _cat = Category.FindFirstRoot();
                _wpage = WebpartPage.FindByPrimaryKey(_cat.ListPage_ID);
                break;
            default:
                _cat = Category.FindByName(pagename);

                if (_cat == null)
                    throw new HttpNotFoundException();

                _isDetail = Request["detail"] != null;
                long wppId = _isDetail ? _cat.DetailPage_ID : _cat.ListPage_ID;
                _wpage = WebpartPage.FindByPrimaryKey(wppId);
                break;
        }

        SetupPage();
    }

    private void SetupPage()
    {
        Session[CoreContext.SESSION_CURRENT_CAT] = _cat;
        var page = new WebpartPageControl { Info = _wpage };
        phContent.Controls.Add(page);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        var output = new StringWriter();
        base.Render(new HtmlTextWriter(output));
        string data = output.ToString();
        if (CoreContext.UseHtmlCache && keyUniq.Length > 0)
        {
            var mExpire = CoreContext.HtmlCacheExpiredSecond / 60;
            CacheHelper.Instance.Set(keyUniq, data, mExpire);
        }
        writer.Write(data);
    }
</script>
